package greate.project.rabbitmq;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DeliverCallback;
import com.rabbitmq.client.ShutdownSignalException;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;

public class RabbitMQClient {

    public static void main(String[] args) throws IOException, TimeoutException {

        // 连接工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();

        // 建立默认连接
        final Connection connection = connectionFactory.newConnection();

        // 创建 channel
        final Channel channel = connection.createChannel();

        channel
                .queueDeclare(
                        "d1",
                        true,
                        false,
                        false,
                        null
                );

        DeliverCallback deliverCallback = (consumerTag, delivery) -> {
            String message = new String(delivery.getBody(), StandardCharsets.UTF_8);
            System.out.println("消费回调" + message);
//            System.out.println(consumerTag);
        };
        final String d1 = channel.basicConsume("d1", true, deliverCallback, (consumerTag, sig) -> {
            final String consumerTag1 = consumerTag;
            final ShutdownSignalException sig1 = sig;
            System.out.println("消费回调1" + sig);

        });

        System.out.println(d1 + " 123" );


        try {
            Thread.sleep(100000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }



}
